:: jimgutil native 库 Android NDK 交叉编译脚本,创建Makefile
:: author guyadong
:: 2020/12/22
:: 可选参数:
::   /NOBUILD        - 只生成Makefile工程文件,不编译项目 
::   /DEBUG          - 生成Debug版工程 \
:: 编译 arm5(armeabi) 平台版本要求　NDK 16 及以下版本.

@ECHO off
SETLOCAL 
:: 检测是否安装NDK,没有安装NDK则报错退出
IF NOT DEFINED ANDROID_NDK (
	ECHO "ERROR:environment variable ANDROID_NDK not define" 
    EXIT /B 255
	)
WHERE cmake
if ERRORLEVEL 1 (
	ECHO cmake NOT FOUND.
	EXIT /B -1
)
ECHO cmake found
WHERE java
if ERRORLEVEL 1 (
	ECHO java NOT FOUND.
	EXIT /B -1
)
ECHO java found
SET sh_folder=%~dp0
:: 定义编译的版本类型(DEBUG|RELEASE)
SET build_type=RELEASE

SET OPT_BUILD=1
:: parse command arguments
:loop
IF x%1 == x goto :pare_end
IF /I "%1" == "/NOBUILD"    SET OPT_BUILD=0
IF /I "%1" == "/DEBUG"      SET build_type=Debug
SHIFT
goto :loop
:pare_end

ECHO build_type=%build_type%

:: 目标平台
IF NOT DEFINED ANDROID_ABI SET ANDROID_ABI=armeabi-v7a

IF %ANDROID_ABI%==armeabi ( 
    SET ARCH=ARMEABI
) ELSE IF %ANDROID_ABI%==armeabi-v7a ( 
    SET ARCH=ARMEABI-V7A
) ELSE IF %ANDROID_ABI%==arm64-v8a (
    SET ARCH=ARM64-V8A
) ELSE IF %ANDROID_ABI%==x86 (
    SET ARCH=X86
) ELSE IF %ANDROID_ABI%==x86_64 (
    SET ARCH=x64
) ELSE (
    ECHO Invalid Android ABI: %ANDROID_ABI%
    EXIT /B 255
)

:: 编译输出路径
SET BUILDDIR=%sh_folder%build\jimgutil_android\%ANDROID_ABI%
SET OUTDIR=%sh_folder%release\jimgutil_android\%ANDROID_ABI%

ECHO =========================================================================
ECHO     Configuration: jimgutil %ANDROID_ABI%:%build_type%
ECHO   Build Directory: %BUILDDIR%
ECHO Install Directory: %OUTDIR%
ECHO =========================================================================

IF EXIST "%BUILDDIR%" ( RMDIR "%BUILDDIR%" /s/q || EXIT /B )
MKDIR "%BUILDDIR%" || EXIT /B

PUSHD "%BUILDDIR%" || EXIT /B

cmake %sh_folder% -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=%build_type% ^
	-DCMAKE_INSTALL_PREFIX=%OUTDIR% ^
	-DANDROID_ARM_NEON=ON ^
	-DANDROID_STL=c++_static ^
    -DANDROID_ABI=%ANDROID_ABI% ^
	-DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%\build\cmake\android.toolchain.cmake || EXIT /B

SET dst_dir=%sh_folder%..\jimgutil\src\main\resources\lib\%ANDROID_ABI%

IF %OPT_BUILD% == 1 (
    cmake --build . --target install/strip -- -j8 || EXIT /B
    IF NOT EXIST %dst_dir% ( MKDIR %dst_dir% || EXIT /B )
    COPY /Y /V %OUTDIR%\lib\libjimgutil.so %dst_dir%
)

POPD
ENDLOCAL